package com.ztjava.tieba.controller;

import com.jfinal.aop.Duang;
import com.jfinal.kit.Ret;
import com.ztjava.core.Constant;
import com.ztjava.core.annotation.ControllerClass;
import com.ztjava.core.base.BaseController;
import com.ztjava.core.client.PortalClient;
import com.ztjava.core.tool.ConfigTool;
import com.ztjava.ext.auth.annotation.UserController;
import com.ztjava.ext.auth.annotation.UserMethod;
import com.ztjava.ext.auth.bean.AuthSession;
import com.ztjava.tieba.model.TiebaCatalog;
import com.ztjava.tieba.model.TiebaContent;
import com.ztjava.tieba.model.TiebaReply;
import com.ztjava.tieba.model.TiebaSubject;
import com.ztjava.tieba.service.TiebaService;
@ControllerClass(client=PortalClient.class,path="/tieba")
@UserController(portal=Constant.P_PORTAL)
public class TiebaPortalController extends BaseController {
	/*************
	 *TiebaAdminController.java
	 *@ztjava.com
	 *2018-04-26 10:04:12
	**************/
	private static TiebaService tiebaService = Duang.duang(TiebaService.class);
	/*************
	 *修改Subject
	**************/
	@UserMethod("TiebaSubject")
	public void addEditSubject(){
		Integer id = getParaToInt("id",0);
		TiebaSubject tiebaSubject = new TiebaSubject();
		if(id>0){
			tiebaSubject = TiebaSubject.dao.findById(id);
		}
		setAttr("tiebaSubject",tiebaSubject);
		render("addEditSubject.html");
	}

	/*************
	 *保存Subject
	**************/
	@UserMethod("TiebaSubject")
	public void saveSubject(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		TiebaSubject tiebaSubject = getModel(TiebaSubject.class);
		Ret ret = tiebaService.checkRepeat(tiebaSubject,TiebaSubject.tablename
			,"",new String[]{"code","代码不能重复"});
		if(ret.get(Constant.RESULT).equals(Constant.ERROR)){
			renderError(ret.get(Constant.MESSAGE).toString());
			return;
		}
		saveModel(tiebaSubject,authSession.getId());
		
		ConfigTool.dbToBaseData("TIEBA_SUBJECT", TiebaSubject.tablename, "id asc");
		renderSuccess();
	}

	/*************
	 *Subject列表
	**************/
	@UserMethod("TiebaSubject")
	public void subjectList(){
		
		if(!getPara("is_ajax","F").equals("T")){
			keepPara();
			render("subjectList.html");
			return;
		}

		
		renderPageJson(tiebaService.getSubjectPage(client, getParaMap()));
	}

	/*************
	 *批量删除Subject
	**************/
	@UserMethod("TiebaSubject")
	public void batchDeleteSubject(){
		String[] btSelectItem = getParaValues("btSelectItem");
		tiebaService.batchDelete(TiebaSubject.tablename, btSelectItem);
		ConfigTool.dbToBaseData("TIEBA_SUBJECT", TiebaSubject.tablename, "id asc");
		renderSuccess();
	}
	
	/*************
	 *修改Catalog
	**************/
	@UserMethod("TiebaSubject")
	public void addEditCatalog(){
		Integer id = getParaToInt("id",0);
		TiebaCatalog tiebaCatalog = new TiebaCatalog();
		if(id>0){
			tiebaCatalog = TiebaCatalog.dao.findById(id);
		}else{
				tiebaCatalog.set("subject_id",getParaToInt("subject_id",0));
		}
		setAttr("tiebaCatalog",tiebaCatalog);
		render("addEditCatalog.html");
	}

	/*************
	 *保存Catalog
	**************/
	@UserMethod("TiebaSubject")
	public void saveCatalog(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		TiebaCatalog tiebaCatalog = getModel(TiebaCatalog.class);
		saveModel(tiebaCatalog,authSession.getId());
		ConfigTool.dbToBaseData("TIEBA_CATALOG", TiebaCatalog.tablename, "sort asc,id asc");
		renderSuccess();
	}

	/*************
	 *Catalog列表
	**************/
	@UserMethod("TiebaSubject")
	public void catalogList(){
		Integer subject_id = getParaToInt("subject_id");
		setAttr("subject_id",subject_id);
		if(!getPara("is_ajax","F").equals("T")){
			keepPara();
			render("catalogList.html");
			return;
		}

		
		renderPageJson(tiebaService.getCatalogPage(client, getParaMap()));
	}

	/*************
	 *批量删除Catalog
	**************/
	@UserMethod("TiebaSubject")
	public void batchDeleteCatalog(){
		String[] btSelectItem = getParaValues("btSelectItem");
		tiebaService.batchDelete(TiebaCatalog.tablename, btSelectItem);
		ConfigTool.dbToBaseData("TIEBA_CATALOG", TiebaCatalog.tablename, "sort asc,id asc");
		renderSuccess();
	}
	
	/*************
	 *修改Content
	**************/
	@UserMethod("TiebaContent")
	public void addEditContent(){
		Integer id = getParaToInt("id",0);
		TiebaContent tiebaContent = new TiebaContent();
		if(id>0){
			tiebaContent = TiebaContent.dao.findById(id);
		}
		setAttr("tiebaContent",tiebaContent);
		render("addEditContent.html");
	}

	/*************
	 *保存Content
	**************/
	@UserMethod("TiebaContent")
	public void saveContent(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		TiebaContent tiebaContent = getModel(TiebaContent.class);
		saveModel(tiebaContent,authSession.getId());
			renderSuccess();
	}

	/*************
	 *Content列表
	**************/
	@UserMethod("TiebaContent")
	public void contentList(){
		
		if(!getPara("is_ajax","F").equals("T")){
			keepPara();
			render("contentList.html");
			return;
		}

		
		renderPageJson(tiebaService.getContentPage(client, getParaMap()));
	}

	/*************
	 *批量删除Content
	**************/
	@UserMethod("TiebaContent")
	public void batchDeleteContent(){
		String[] btSelectItem = getParaValues("btSelectItem");
		tiebaService.batchDelete(TiebaContent.tablename, btSelectItem);
		renderSuccess();
	}
	
	/*************
	 *修改Reply
	**************/
	@UserMethod("TiebaReply")
	public void addEditReply(){
		Integer id = getParaToInt("id",0);
		TiebaReply tiebaReply = new TiebaReply();
		if(id>0){
			tiebaReply = TiebaReply.dao.findById(id);
		}
		setAttr("tiebaReply",tiebaReply);
		render("addEditReply.html");
	}

	/*************
	 *保存Reply
	**************/
	@UserMethod("TiebaReply")
	public void saveReply(){
		AuthSession authSession = getAttr(Constant.AUTHSESSION);
		TiebaReply tiebaReply = getModel(TiebaReply.class);
		saveModel(tiebaReply,authSession.getId());
			renderSuccess();
	}

	/*************
	 *Reply列表
	**************/
	@UserMethod("TiebaReply")
	public void replyList(){
		
		if(!getPara("is_ajax","F").equals("T")){
			keepPara();
			render("replyList.html");
			return;
		}

		
		renderPageJson(tiebaService.getReplyPage(client, getParaMap()));
	}

	/*************
	 *批量删除Reply
	**************/
	@UserMethod("TiebaReply")
	public void batchDeleteReply(){
		String[] btSelectItem = getParaValues("btSelectItem");
		tiebaService.batchDelete(TiebaReply.tablename, btSelectItem);
		renderSuccess();
	}
}
